Re: [SQL] Odd characters in inserted data... - Mailing list pgsql-interfaces

From Herouth Maoz
Subject Re: [SQL] Odd characters in inserted data...
Date
Msg-id l03110701b288800a97e0@[147.233.159.109]
Whole thread Raw
List pgsql-interfaces
(Redirected to the INTERFACES list, which is more appropriate for this than
the SQL list)

At 18:09 +0200 on 30/11/98, Tom Lane wrote:


> This is a lot easier to get right in C++, where the function can return
> a temporary string object that will get deallocated automatically after
> use.  In C the most reasonable way to proceed is to have SQLstring
> return a pointer to a static area, which means you can't invoke
> SQLstring more than once per sprintf :-(.  You end up with klugery like
>     sprintf(query, "update tablename set field1 = %s,",
>         SQLstring(field1));
>     sprintf(query + strlen(query), " field2 = %s where ...",
>         SQLstring(field2), ...);
> Ugh.  It works (I've done it a lot), but it's not pretty, and it's
> easy to get wrong.

If it were me, I'd malloc the pointer inside the function, and explicitly
free it after sprintf'ing. If you want to be more elegant (in C???), you
can malloc it in advance, pass the malloced pointer to the function and
free it afterwards, to make it consistent with the way str functions work.

I should think this was a good function to add to libpq. At least, the part
that does the quoting, maybe without the other frills. It's a function
*everybody* who uses C will use, and it makes more sense to have it
centralized, so if characters other than backslash and quote need to be
escaped, it will be done within the central source, and not break
everybody's programs.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



pgsql-interfaces by date:

Previous
From: Peter T Mount
Date:
Subject: Re: [INTERFACES] Java, JDBC & CORBA (fwd)
Next
From: Dallas Hockley
Date:
Subject: 1.2 JVM and such